#include "trace_demo.h"

int my_read(int fd, char *buf, int len)
{
	// 追踪读事件,identity为fd，参数为buf、len
	LOS_TRACE(CUSTOM_READ, fd, buf, len);
	for(int i=0; i<len; i++)
		buf[i] = i+1;
	return i;
}

int my_write(int fd, char *buf, int len)
{
	// 追踪写事件,identity为fd，参数为buf、len
	LOS_TRACE(CUSTOM_WRITE, fd, buf, len);
	for(int i=0; i<len; i++)
		printf("write char %d\n", buf[i]);
	return len;
}

VOID task_read(VOID)
{ 
    UINT32 ret, readLen=0, writeLen=0;
	char buf[20]={0};    
    printf("-----------task trace %d---------\n", LOS_CurTaskIDGet());
	
    // 开启trace
    ret = LOS_TraceStart();   
    if (ret != LOS_OK) {        
        printf("trace start error\n");        
        return;    
    }   
    // 读取数据
	while(readLen<20){
		readLen += my_read(1, buf+readLen, 5);
		// 写入数据
		while(writeLen<readLen){
			writeLen += my_write(2, buf+writeLen, 5);
		}
	}
    // 停止trace    
    LOS_TraceStop();    
	// 输出数据
    LOS_TraceRecordDump(FALSE);
}

UINT32 trace_entry_stand(VOID){
    UINT32 ret, tid;    

    // 系统默认情况下已启动trace, 因此可先关闭trace后清除缓存区后，再重启trace                   	  
    LOS_TraceStop();    
    LOS_TraceReset();    
	// 设置掩码，只追踪简易插桩
	LOS_TraceEventMaskSet(TRACE_CUSTOM_FLAG);  
    TSK_INIT_PARAM_S param = { 0 };
    // 任务参数   
    memset(&traceTestTask, 0, sizeof(TSK_INIT_PARAM_S));    
    param.pfnTaskEntry = (TSK_ENTRY_FUNC)task_read;    
    param.pcName       = "task_trace";    /* 测试任务名称 */    				     
    param.uwStackSize  = 0x2000;    
    param.usTaskPrio   = 10;    
    param.uwResved = 0x0100;
    // 锁住任务调度，防止高优先级任务调度
    LOS_TaskLock();
    // 创建任务  
    ret = LOS_TaskCreate(&tid, &param);    
    if(ret != LOS_OK){        
        printf("TraceTestTask create failed %d.\n", ret);          
    }    
    // 解锁任务调度
    LOS_TaskUnlock();
    return ret;
}
SYS_RUN(trace_entry_stand);